package io.yizoo.system.controller;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.elm.core.CoreController;
import io.yizoo.system.entity.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.EnumSet;
import java.util.List;

@RestController
@RequestMapping("/system/role")
public class roleController extends CoreController {

    @Override
    protected void Init() {
        PowerEntity = sys_role.class;
        PowerUse = EnumSet.of(Power.IDDelete, Power.IDAny, Power.IDGetSingle,Power.IDUpsert);
    }

    @PostMapping("/GetData")
    public Object GetData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .From(sys_role.class)
                        .IsWhere("and name like CONCAT('%', #{name}, '%')","name")
                        .IsPage()
                        .GetFactor());
        if(Ctor.isPage(jsonStr)) {
            return JsonResultPage(ret);
        }
        else {
            return JsonResult(ret);
        }
    }

    @PostMapping("/GetRoleMenu")
    public Object GetRoleMenu(@RequestBody String jsonStr) {
        Object ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .Select("t.id")
                        .From(sys_menu.class,"t")
                        .LeftJoin(sys_role_rel_menu.class,"a","a.menu_id=t.id")
                        .Where("and a.role_id=#{role_id}","role_id")
                        .GetFactor());
        return JsonResult(ret);
    }

    @PostMapping("/SaveRoleMenu")
    public Object SaveRoleMenu(@RequestBody String jsonStr) {
        ObjectNode node = Ctor.getObjectNode(jsonStr);
        Object ret = Ctor.HMapper.Save(sys_role_rel_menu.class,(ArrayNode) node.get("items"),
                Ctor.DMaker().Deleter
                        .putFactorItem("role_id",node.get("role_id").asText())
                        .From(sys_role_rel_menu.class)
                        .Where("and role_id=#{role_id}","role_id")
                        .GetFactor()
                );
        return JsonResult(ret);
    }
    @PostMapping("/SaveRoleUser")
    public Object SaveRoleUser(@RequestBody String jsonStr) {
        ObjectNode node = Ctor.getObjectNode(jsonStr);
        Object ret = Ctor.HMapper.Save(sys_role_rel_user.class,(ArrayNode) node.get("items"),
                Ctor.DMaker().Deleter
                        .putFactorItem("user_id",node.get("user_id").asText())
                        .From(sys_role_rel_user.class)
                        .Where("and user_id=#{user_id}","user_id")
                        .GetFactor()
        );
        return JsonResult(ret);
    }
    @PostMapping("/GetUserRole")
    public Object GetUserRole(@RequestBody String jsonStr) {
        Object data = Ctor.CoreMapper.USelect (
                Ctor.DMaker(jsonStr).Selector.From(sys_role.class,"t")
                        .Select("t.id")
                        .LeftJoin(sys_role_rel_user.class,"a","a.role_id=t.id")
                        .Where("and a.user_id=#{user_id}").GetFactor()
        );
        return JsonResult(data);
    }
}
